<?php
class CustomFPDF extends FPDF {
	var $textHeader = null;
	var $textFooter = null;
	var $B = 0;
	var $I = 0;
	var $U = 0;
	var $HREF = '';
	var $ALIGN = '';
	var $header_config = array (
			'font' => 'Arial',
			'size' => 12,
			'style' => '' 
	);
	var $widths = array ();
	private $header = array ();
	private $config = array ();
	function PDF($orientation = 'P', $unit = 'mm', $size = 'A4') {
		if ($size == 'A4' || $size == "Letter") {
			
			$this->header_config ['size'] = 9;
		}
		parent::FPDF ( $orientation, $unit, $size );
	}
	function add_header($header) {
		$this->header = $header;
	}
	function Header() {
		$this->SetY ( 10 );
		if (count ( $this->header ) > 0 && is_array ( $this->header )) {
			$_widths = $this->widths;
			
			// echo '<pre>';
			foreach ( $this->header as $line ) {
				$this->Ln ();
				$colcountter = 0;
				foreach ( $line as $row ) {
					$this->SetFont ( $row->style->fontname, $row->style->fontstyle, $row->style->fontsize );
					$this->SetFillColor ( $row->style->fillcolor ['r'], $row->style->fillcolor ['g'], $row->style->fillcolor ['b'] );
					$this->SetTextColor ( $row->style->fontcolor ['r'], $row->style->fontcolor ['g'], $row->style->fontcolor ['b'] );
					$width = 0;
					for($w = $colcountter; $w < $colcountter + $row->colspan; $w ++) {
						$width += $_widths [$w];
					}
					$this->Cell ( $width, $row->style->fontsize - 3, $row->data, $row->border, 0, $row->align, true );
					$colcountter ++;
				}
			}
		}
		
		if ($this->PageNo () > 1) {
			$this->Ln ();
		}
		
		/*
		 * $this->SetFont($this->header_config['font'], $this->header_config['style'], $this->header_config['size']); $this->SetTextColor(80); $this->SetY(5); if (!is_null($this->textHeader) && is_array($this->textHeader)) { $this->WriteHTML(str_replace(array('&nbsp;'), ' ', utf8_decode($this->textHeader['_text_topo']))); } if (!is_null($this->textHeader) && is_string($this->textHeader)) { $this->WriteHTML(str_replace(array('&nbsp;'), ' ', utf8_decode($this->textHeader))); } if (isset($this->textHeader['_columns'])) { $i = 1; $this->TableHeader($this->textHeader['_columns']); }
		 */
	}
	function Footer($data = '') {
		// Position at 1.5 cm from bottom
		$this->SetY ( - 15 );
		// Arial italic 8
		$this->SetTextColor ( 140 );
		$this->SetFont ( 'Arial', 'I', 9 );
		// Page number
		if (! is_null ( $this->textFooter )) {
			$this->WriteHTML ( $this->textFooter );
		}
		//$this->Cell ( 0,5, 'gerador' . base_url(),0,0,'L');
		$this->Cell ( 0, 5, 'Gerado ' . date ( 'd/m/Y H:i:s' ) . utf8_decode ( ' Página-' ) . $this->PageNo () . ' / {nb}', 0, 0, 'R' );
		// $this->SetX(300);
		// $this->Cell(0, 10, 'Pagina ' . $this->PageNo() . ' / {nb}', 0, 0, 'R');
	}
	function WriteHTML($html) {
		// HTML parser
		$html = str_replace ( "\n", ' ', $html );
		$a = preg_split ( '/<(.*)>/U', $html, - 1, PREG_SPLIT_DELIM_CAPTURE );
		
		foreach ( $a as $i => $e ) {
			if ($i % 2 == 0) {
				// Text
				if ($this->HREF)
					$this->PutLink ( $this->HREF, $e );
				elseif ($this->ALIGN !== '')
					$this->Cell ( 0, 5, $e, 0, 1, substr ( $this->ALIGN, 0, 1 ) );
				else
					$this->Write ( 5, $e );
			} else {
				// Tag
				if ($e {0} == '/')
					$this->CloseTag ( strtoupper ( substr ( $e, 1 ) ) );
				else {
					// Extract properties
					$a2 = explode ( ' ', $e );
					$tag = strtoupper ( array_shift ( $a2 ) );
					$prop = array ();
					foreach ( $a2 as $v )
						if (preg_match_all ( '^([^=]*)=["\']?([^"\']*)["\']?$', $v, $a3 ))
							$prop [strtoupper ( $a3 [1] )] = $a3 [2];
					$this->OpenTag ( $tag, $prop );
				}
			}
		}
	}
	function OpenTag($tag, $prop) {
		// Opening tag
		if ($tag == 'B' or $tag == 'I' or $tag == 'U')
			$this->SetStyle ( $tag, true );
		if ($tag == 'A')
			$this->HREF = $prop ['HREF'];
		if ($tag == 'BR')
			$this->Ln ( 5 );
		if ($tag == 'P')
			$this->ALIGN = $prop ['ALIGN'];
		if ($tag == 'HR') {
			if ($prop ['WIDTH'] != '')
				$Width = $prop ['WIDTH'];
			else
				$Width = $this->w - $this->lMargin - $this->rMargin;
			$this->Ln ( 2 );
			$x = $this->GetX ();
			$y = $this->GetY ();
			$this->SetLineWidth ( 0.4 );
			$this->Line ( $x, $y, $x + $Width, $y );
			$this->SetLineWidth ( 0.2 );
			$this->Ln ( 2 );
		}
	}
	function CloseTag($tag) {
		// Closing tag
		if ($tag == 'B' or $tag == 'I' or $tag == 'U')
			$this->SetStyle ( $tag, false );
		if ($tag == 'A')
			$this->HREF = '';
		if ($tag == 'P')
			$this->ALIGN = '';
	}
	function SetStyle($tag, $enable) {
		// Modify style and select corresponding font
		$this->$tag += ($enable ? 1 : - 1);
		$style = '';
		foreach ( array (
				'B',
				'I',
				'U' 
		) as $s )
			if ($this->$s > 0)
				$style .= $s;
		$this->SetFont ( '', $style );
	}
	function PutLink($URL, $txt) {
		// Put a hyperlink
		$this->SetTextColor ( 0, 0, 255 );
		$this->SetStyle ( 'U', true );
		$this->Write ( 5, $txt, $URL );
		$this->SetStyle ( 'U', false );
		$this->SetTextColor ( 0 );
	}
	function TableHeader($header) {
		// Header
		$iw = 0;
		
		if (count ( $header ) && is_array ( $header )) :
			foreach ( $header as $col ) {
				$w = $col ['width'];
				$iw += $col ['width'];
				$this->Cell ( $col ['width'], 8, $col ['label'], 1 );
			}
			$this->Ln ();
		
		
		
		
        endif;
	}
}

